/**
 * Created by lional on 16/10/2.
 */

jGame.ImageView = ccui.ImageView.extend(/** @lends jGame.ImageView# */{

    _controller:null,
    _isLongClickEnabled: null,
    _longClickAction: null,
    _longClickName: null,
    _sendLongClick: null,
    _touchTime:null,

    ctor: function (imageFileName, texType) {
        this._super(imageFileName, texType);
        //cc.log("jGame.ImageView#ctor()");
    },

    setController: function(controller){
        this._controller = controller;
    },

    getController: function(){
        return this._controller;
    },

    _checkLongClickToucheBegan: function (sender, type) {
        if(type == ccui.Widget.TOUCH_BEGAN) {
            sender._touchTime = new Date().getTime();
            this.stopAction(this._longClickAction);

            var widget = this;
            var longClickCallBack = function (sender) {
                if(widget["_controller"]) {
                    if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, ccui.Widget.TOUCH_ENDED);
                    if(typeof widget["_controller"][widget["_longClickName"]] === "function")widget["_controller"][widget["_longClickName"]](sender, ccui.Widget.TOUCH_ENDED);
                    widget._sendLongClick = true;
                }
            }
            this._longClickAction = cc.sequence(
                cc.delayTime(jGame.LONG_CLICK_TIME/1000),
                cc.callFunc(longClickCallBack, this)  // If you want to pass a optional value, like "Hi!", then you should pass 'this' too
            );
            this.runAction(this._longClickAction);
            widget._sendLongClick = false;
        }else if(type == ccui.Widget.TOUCH_ENDED) {
            setTimeout(function () {
                sender._touchTime = 0;
            }, 0)
        }
    },

    isLongClick: function () {
        if(this._touchTime) {
            var timestamp = new Date().getTime();
            if(timestamp - this._touchTime > jGame.LONG_CLICK_TIME) {
                return true;
            }
        }
        return false;
    },

    setLongClickEnabled: function (enabled) {
        this._isLongClickEnabled = enabled?true:false;
    },

    setTouchCallbackName: function (callBackName) {
        this._longClickName = callBackName;
        if(callBackName === undefined)
        {
            this.addTouchEventListener(callBackName);
            return;
        }
        var widget = this;
        var callBack = function(sender, type){
            if(type == ccui.Widget.TOUCH_BEGAN) {
                if (widget._isLongClickEnabled && typeof widget["_checkLongClickToucheBegan"] === "function") {
                    widget["_checkLongClickToucheBegan"](sender, type);
                }
            }else if(type == ccui.Widget.TOUCH_MOVED) {
                if(widget._longClickAction) {
                    widget.stopAction(widget._longClickAction);
                }
            }
            if(widget._sendLongClick) {
                widget._sendLongClick = false;
            }else {
                if (typeof widget["_controller"] === "object" && typeof widget["_controller"][callBackName] === "function") {
                    if (typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, type);
                    widget["_controller"][callBackName](sender, type);
                }
                else if (typeof widget[callBackName] === "function")
                    widget[callBackName](sender, type);
            }
        };
        this.setTouchEnabled(true);
        this.addTouchEventListener(callBack);
    },

});
